package com.lkd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.dto.RegionDto;
import com.lkd.entity.Business;
import com.lkd.entity.Node;
import com.lkd.entity.Region;
import com.lkd.mapper.BusinessMapper;
import com.lkd.mapper.NodeMapper;
import com.lkd.mapper.RegionMapper;
import com.lkd.mapper.VendingMachineMapper;
import com.lkd.result.PageBean;
import com.lkd.service.RegionService;
import com.lkd.vo.NodeVo;
import com.lkd.vo.RegionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private NodeMapper nodeMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private VendingMachineMapper vendingMachineMapper;


    @Override
    public PageBean findByPage(RegionDto regionDto) {
        //开启分页
        PageHelper.startPage(regionDto.getPageIndex(), regionDto.getPageSize());

        //执行查询
        List<Region> list = regionMapper.findList(regionDto);
        Page<Region> page = (Page<Region>) list;

        //处理每个区域下的点位数
        List<RegionVo> regionVoList = list.stream().map(region -> {
            RegionVo regionVo = BeanUtil.copyProperties(region, RegionVo.class);
            Integer count = nodeMapper.countByRegionId(region.getId());
            regionVo.setNodeCount(count);
            return regionVo;
        }).collect(Collectors.toList());


        //返回结果
        PageBean<RegionVo> pageBean = new PageBean<>();
        pageBean.setPageIndex(page.getPageNum());
        pageBean.setPageSize(page.getPageSize());
        pageBean.setTotalPage((long) page.getPages());
        pageBean.setTotalCount(page.getTotal());
        pageBean.setCurrentPageRecords(regionVoList);

        return pageBean;
    }

    @Override
    public Boolean save(Region region) {
        return regionMapper.save(region);
    }

    @Override
    public Boolean update(Region region) {
        return regionMapper.update(region);
    }

    @Override
    public PageBean<NodeVo> findNodeByRegionId(RegionDto regionDto) {
        PageHelper.startPage(regionDto.getPageIndex(), regionDto.getPageSize());

        List<Node> nodeList = nodeMapper.findList(regionDto);
        Page<Node> page = (Page<Node>) nodeList;

        List<NodeVo> nodeVoList = nodeList.stream().map(node -> {
            //根据区域id搞定区域VO
            Long regionId = node.getRegionId();
            Region region = regionMapper.findById(regionId);
            RegionVo regionVo = BeanUtil.copyProperties(region, RegionVo.class);
            Integer count = nodeMapper.countByRegionId(region.getId());
            regionVo.setNodeCount(count);

            //根据商圈id搞定商圈对象
            Integer businessId = node.getBusinessId();
            Business business = businessMapper.findById(businessId);

            //根据nodeid统计机器数量
            Integer vmCount = vendingMachineMapper.countByNodeId(node.getId());


            //组装返回
            NodeVo nodeVo = BeanUtil.copyProperties(node, NodeVo.class);
            nodeVo.setRegion(regionVo);
            nodeVo.setBusinessType(business);
            nodeVo.setVmCount(vmCount);

            return nodeVo;
        }).collect(Collectors.toList());


        PageBean<NodeVo> pageBean = new PageBean<>();
        pageBean.setPageIndex(page.getPageNum());
        pageBean.setPageSize(page.getPageSize());
        pageBean.setTotalPage((long) page.getPages());
        pageBean.setTotalCount(page.getTotal());
        pageBean.setCurrentPageRecords(nodeVoList);
        return pageBean;
    }
}
